package com.evil.enterprise.controller;

import com.evil.application.api.RemoteAppOrderOperationService;
import com.evil.application.pojo.dto.form.ControlDataRespDTO;
import com.evil.application.pojo.dto.order.exp.OrderExportReqDTO;
import com.evil.application.pojo.dto.order.imp.ExcelImportReqDTO;
import com.evil.application.util.ExportOrderUtil;
import com.evil.common.application.dto.ApplicationKeyReqDTO;
import com.evil.common.core.entity.QueryModel;
import com.evil.common.core.entity.R;
import com.evil.common.file.dto.FileRespDTO;
import com.evil.common.file.util.PoiUtil;
import com.evil.common.log.annotation.RequestLogAnnotation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 应用导出接口
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/application")
public class ApplicationExportController {

    private final RemoteAppOrderOperationService remoteAppOrderOperationService;

    /**
     * 下载导入应用订单模板
     *
     * @param applicationKeyReqDTO applicationKeyReqDTO
     */
    @PostMapping("/order/download/template")
    public void applicationDownloadTemplate(@Validated @RequestBody ApplicationKeyReqDTO applicationKeyReqDTO) {
        ControlDataRespDTO controlDataRespDTO = remoteAppOrderOperationService.downloadTemplate(applicationKeyReqDTO);
        Workbook workbook = ExportOrderUtil.createAppOrderWorkbook(controlDataRespDTO.getControlList(), controlDataRespDTO.getApplicationName());
        PoiUtil.outputStreamExcel(workbook, controlDataRespDTO.getApplicationName(), true);
    }

    /**
     * 应用订单导出
     *
     * @param queryModel queryModel
     */
    @PostMapping("/order/export")
    public R<FileRespDTO> orderDataExport(@Validated @RequestBody QueryModel<OrderExportReqDTO> queryModel) {
        remoteAppOrderOperationService.orderDataExport(queryModel);
        return R.success();
    }

    /**
     * 应用订单导入
     *
     * @param excelImportReqDTO excelImportReqDTO
     * @return R
     */
    @RequestLogAnnotation(isSaveRequestData = false)
    @PostMapping("/order/import")
    public R<?> orderDataImport(@Validated ExcelImportReqDTO excelImportReqDTO) {
        remoteAppOrderOperationService.orderDataImport(excelImportReqDTO);
        return R.success();
    }
}
